package com.ziq.base.view.webview

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.ziq.base.R
import com.ziq.base.base.BaseActivity
import com.ziq.base.databinding.FwActivityBaseFragmentBinding
import com.ziq.base.extension.binding

open class BaseWebViewActivity : BaseActivity() {

    companion object {
        fun openUrl(context: Context, url : String, isShowToolbar : Boolean = true){
            val intent = Intent(context, BaseWebViewActivity::class.java)
            intent.putExtra(BaseWebViewFragment.TARGET_URL, url)
            intent.putExtra(BaseWebViewFragment.IS_SHOW_TOOLBAR, isShowToolbar)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(intent)
        }
    }

    private val binding : FwActivityBaseFragmentBinding by binding()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.root

        var targetFragment = supportFragmentManager.findFragmentByTag("BaseWebViewFragment")
        if(targetFragment == null){
            targetFragment = BaseWebViewFragment(R.layout.fw_fragment_base_webview)
        }
        targetFragment.arguments = Bundle().apply {
            this.putString(BaseWebViewFragment.TARGET_URL, intent.getStringExtra(BaseWebViewFragment.TARGET_URL)?:"")
            this.putBoolean(
                BaseWebViewFragment.IS_SHOW_TOOLBAR, intent.getBooleanExtra(
                    BaseWebViewFragment.IS_SHOW_TOOLBAR, true))
        }
        supportFragmentManager.beginTransaction()
            .replace(R.id.fl_fragment_container, targetFragment, "BaseWebViewFragment")
            .commit()

    }

}